package com.apollow.oschina.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.base.BaseFragment;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.FriendBean;
import com.apollow.oschina.protocol.FollowProtocol;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Dragon on 2017/1/16.
 */

public class FollowFragment extends BaseFragment {
    RecyclerView mRvFollow;
    private List<FriendBean> friends;
    private FollowAdapter mFollowAdapter;
    private FollowProtocol mFansProtocol;
    private LinearLayoutManager mManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = Global.layoutInflate(R.layout.fragment_follow);
        ButterKnife.bind(this, view);
        mRvFollow = (RecyclerView) view.findViewById(R.id.rv_follow);
        mManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRvFollow.setLayoutManager(mManager);
        mFollowAdapter = new FollowAdapter();
        mRvFollow.setAdapter(mFollowAdapter);
        return view;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        mFansProtocol = new FollowProtocol();
        new Thread(new Runnable() {
            @Override
            public void run() {
                friends = mFansProtocol.getData().getFriends();
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mFollowAdapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    public class FollowAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = Global.inflate(R.layout.item_fragment_follow, parent);
            ViewHolder holder = new ViewHolder(itemView);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder holderview = (ViewHolder) holder;
            holderview.setData(friends.get(position));
        }

        @Override
        public int getItemCount() {
            return friends == null ? 0 : friends.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.civ_header)
            CircleImageView mCivHeader;
            @BindView(R.id.tv_username)
            TextView mTvUsername;
            @BindView(R.id.iv_male)
            ImageView mIvMale;
            @BindView(R.id.tv_adress)
            TextView mTvAdress;
            @BindView(R.id.tv_work)
            TextView mTvWork;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void setData(FriendBean friendBean) {
                mTvUsername.setText(friendBean.getName());
                mTvAdress.setText(friendBean.getFrom());
                mTvWork.setText(friendBean.getExpertise());
            }
        }
    }
}
